package com.wu.search.util;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class PinYinUtils {
    final static String regEx = "[\n`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】《》‘；：”“’。，、？]";

    public static String getPinyin(String text) {

        char[] chars = text.toCharArray();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        // 设置大小写
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 设置声调表示方法
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        // 设置字母u表示方法
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        String[] s;
        String rs = StringUtils.EMPTY;
        try {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < chars.length; i++) {
                // 判断是否为汉字字符
                if (String.valueOf(chars[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    s = PinyinHelper.toHanyuPinyinStringArray(chars[i], format);
                    if (s != null && s.length > 0) {
                        sb.append(s[0]).append(" ");
                        continue;
                    }
                }
                sb.append(chars[i]);
                if ((i + 1 >= chars.length) || String.valueOf(chars[i + 1]).matches("[\\u4E00-\\u9FA5]+")) {
                    sb.append(" ");
                }
            }
            if (StringUtils.isNotEmpty(sb.toString())) {
                rs = sb.substring(0, sb.length() - 1);
            } else {
                rs = text;
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            log.warn(" pinyin 转换失败 keyword = {} \n error = {} ", text, e.getMessage());
        }
        String str = rs.replaceAll(regEx, "").trim();
        return str.replaceAll(" +", " ");
    }

    /*
     * 拼音中间不带空格
     * */
    public static String getPinyinV2(String china, String region) {
        // 非CN 区域不走拼音查询
        if (!region.equals("CN")) {
            return "";
        }
        HanyuPinyinOutputFormat formart = new HanyuPinyinOutputFormat();
        formart.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        formart.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        formart.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] arrays = china.trim().toCharArray();
        String result = "";
        try {
            for (int i = 0; i < arrays.length; i++) {
                char ti = arrays[i];
                if (Character.toString(ti).matches("[\\u4e00-\\u9fa5]")) { //匹配是否是中文
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(ti, formart);
                    result += temp[0];
                } else {
                    result += ti;
                }
            }
        } catch (Exception e) {
//            e.printStackTrace();
            return "";
        }
        return result.replaceAll(regEx, "").replaceAll(" ", "");
    }

    public static boolean isAllChinese(String text) {
        if (StringUtils.isEmpty(text)) {
            return false;
        }
        return text.matches("^[\\u4e00-\\u9fa5]+$");
    }

}
